import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/api/interface/home_api.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/user_data_entity.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/page/min/min_collection_page.dart';
import 'package:lcapp/page/min/min_video_page.dart';
import 'package:lcapp/page/min/min_xianhuo_page.dart';
import 'package:lcapp/widget/asr_cache_image.dart';
import 'package:url_launcher/url_launcher.dart';

class ShopHomePage extends StatefulWidget {
  final int uid;
  const ShopHomePage({Key? key, required this.uid}) : super(key: key);

  @override
  _ShopHomePageState createState() => _ShopHomePageState();
}

class _ShopHomePageState extends State<ShopHomePage> with SingleTickerProviderStateMixin{
  late TabController _tabController;

  UserDataEntity user = UserDataEntity();
  ScrollController _scrollController = ScrollController();
  GlobalKey _tabKey = GlobalKey();
  double tabHeight = 30.w;

  @override
  void initState() {
    // TODO: implement initState
    _tabController = TabController(length: 4, vsync: this);
    getUserInfo();
    _scrollController.addListener(_scListiener);
  }

  void _scListiener(){
    final RenderBox renderBox = _tabKey.currentContext?.findRenderObject() as RenderBox;
    Offset offset = renderBox.localToGlobal(Offset.zero);
    print('offset========' + (offset.dy + renderBox.size.height).toString());
    print('status========' + ScreenUtil().statusBarHeight.toString());
    setState(() {
      if(offset.dy + renderBox.size.height <= 0.w){
        tabHeight = ScreenUtil().statusBarHeight;
      }else{
        tabHeight = 30.w;
      }
    });
  }

  getUserInfo()async{
    ResponseData res = await UserAPI.getUserInfo(widget.uid);
    if(res.code==200&&res.data!=null){
      user = UserDataEntity().fromJson(res.data);
      if(user.address!=null){
        user.addressRegin = '${user.address?.province ??''} ${user.address?.city ??''} ${user.address?.region ??''}' ;
        user.addressDetail = user.address?.detailAddress ??'';
        user.region = user.address?.region ??'';
        user.coordinate = '${user.address?.longitude??'0'},${user.address?.latitude??'0'}';
      }
    }
    if(mounted){setState(() {});}
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _scrollController.removeListener(_scListiener);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NestedScrollView(
        controller: _scrollController,
        headerSliverBuilder: (context,vale){
          return [
            SliverToBoxAdapter(
              child: Container(
                width: 1.sw,
                key: _tabKey,
                child: Stack(
                  children: [
                    Container(
                      child: Image.asset('assets/min/min_top_bg.png',width: 1.sw,fit: BoxFit.fill,height: ScreenUtil().statusBarHeight+600.w,),
                    ),
                    Container(
                      // color: Colors.green,
                      margin: EdgeInsets.only(top: 120.w+ScreenUtil().statusBarHeight),
                      height: 150.w,
                      padding: EdgeInsets.symmetric(horizontal: 50.w),
                      child: Row(
                        children: [
                          Container(
                            height: 150.w,
                            width: 150.w,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(150.w,)
                            ),
                            child: Container(
                              width: 140.w,
                              height: 140.w,
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(140.w,),
                                child: ASRCacheImage(
                                  image: user.img??'assets/icon/p_head.png',
                                  fit: BoxFit.fitWidth,
                                  width: 140.w,
                                  height: 140.w,
                                ),
                              ),
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 80.w),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text('${user.like}',style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 60.sp,
                                    fontWeight: FontWeight.w600
                                ),),
                                SizedBox(height:20.w),
                                Text('收到意向',style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 30.sp,
                                ),)
                              ],
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 80.w),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text('${user.fans}',style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 60.sp,
                                    fontWeight: FontWeight.w600
                                ),),
                                SizedBox(height:20.w),
                                Text('粉丝',style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 30.sp,
                                ),)
                              ],
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 80.w),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text('${user.attention}',style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 60.sp,
                                    fontWeight: FontWeight.w600
                                ),),
                                SizedBox(height:20.w),
                                Text('关注',style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 30.sp,
                                ),)
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 30.w,right: 30.w,top: 320.w+ScreenUtil().statusBarHeight,),
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10.w)
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            child: Row(
                              children: [
                                Expanded(child: Container(child: Column(
                                  children: [
                                    Container(
                                      child: Row(
                                        children: [
                                          Text('${user.name}',style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 40.sp,
                                              fontWeight: FontWeight.w600
                                          ),),
                                          SizedBox(width: 10.w,),
                                          SizedBox(width: 10.w,),
                                          Visibility(child: Image.asset('assets/min/min_sm.png',height: 30.w,),visible: user.card==1,),
                                          SizedBox(width: 10.w,),
                                          Visibility(child: Image.asset('assets/min/min_vip.png',height: 30.w,),visible: user.vip==1,),
                                        ],
                                      ),
                                    ),
                                    SizedBox(height: 10.w,),
                                    Visibility(child: Container(
                                      child: Row(
                                        children: [
                                          Image.asset('assets/home/smrz_succ.png',height: 26.w,),
                                          SizedBox(width: 10.w,),
                                          Text(user.realName??'',style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 24.sp,
                                              fontWeight: FontWeight.w400
                                          ),),
                                        ],
                                      ),
                                    ),visible: user.card==1,),
                                    SizedBox(height: 10.w,),
                                    Visibility(child: Container(
                                      child: Row(
                                        children: [
                                          Image.asset('assets/min/min_company.png',height: 30.w,),
                                          SizedBox(width: 10.w,),
                                          Text(user.company??'',style: TextStyle(
                                            color: Color(0xff5A5A5A),
                                            fontSize: 26.sp,
                                          ),),
                                        ],
                                      ),
                                    ),visible: user.company!=null&&user.company!.isNotEmpty,)
                                  ],
                                ),)),
                              ],
                            ),
                          ),
                          Container(
                            height: 0.5,
                            margin: EdgeInsets.symmetric(vertical: 25.w),
                            color: Color(0xffE9E8E9),
                          ),
                          Container(
                            alignment: Alignment.centerLeft,
                            child: Text('${user.profiles!=null? user.profiles!.trim().length==0?'个人介绍...':user.profiles:'个人介绍...'}',
                              style: TextStyle(
                                color: Color(0xff616161),
                                fontSize: 30.sp,
                                fontWeight: FontWeight.w500,
                              ),
                              maxLines: 2,
                            ),
                          ),
                          SizedBox(height: 20.w,),
                          Container(
                            child: Row(
                              children: [
                                Container(
                                  decoration: BoxDecoration(
                                      color: Color(0xffE4E4E4),
                                      borderRadius: BorderRadius.circular(8.w)
                                  ),
                                  padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 10.w),
                                  child: Row(
                                    children: [
                                      Image.asset(user.sex!=1? 'assets/min/min_boy.png':'assets/min/min_gir.png',width: 20.w,),
                                      SizedBox(width: 10.w,),
                                      Text('${user.addressRegin??'德州'}',style: TextStyle(
                                          color: Color(0xff616161),
                                          fontSize: 20.sp,
                                          fontWeight: FontWeight.w500
                                      ),),
                                    ],
                                  ),
                                ),
                                Expanded(child: Container())
                              ],
                            ),
                          ),
                          SizedBox(height: 50.w,),
                          Container(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Visibility(child: GestureDetector(
                                  onTap: ()async{
                                    showLoading();
                                    ResponseData res = await UserAPI.followUser(widget.uid);
                                    hideLoading();
                                    if(res.code==200){
                                      setState(() {
                                        if(user.attentionState==1){
                                          user.fans = user.fans! - 1;
                                          user.attentionState=0;
                                          showToast('已取消关注');
                                        }else{
                                          showToast('已关注');
                                          user.fans = user.fans! + 1;
                                          user.attentionState=1;
                                        }
                                      });
                                    }
                                  },
                                  child: Container(
                                    alignment: Alignment.center,
                                    child: user.attentionState==1? Container(
                                      width: 280.w,
                                      height: 70.w,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                          color: Color(0xffE4E4E4),
                                          borderRadius: BorderRadius.circular(8.w)
                                      ),
                                      child: Text('已关注',style: TextStyle(
                                          color: Color(0xff616161),
                                          fontSize: 26.sp,
                                          fontWeight: FontWeight.w500
                                      ),),
                                    ) : Image.asset('assets/min/min_add_gz.png',width: 280.w,),
                                  ),
                                ),visible:true,),
                                Visibility(
                                  child: GestureDetector(
                                    onTap: ()async{
                                      if(userInfo.vip!=1){
                                        SmartDialog.show(
                                          alignment: Alignment.center,
                                          clickMaskDismiss: true,
                                          maskColor: Color.fromRGBO(0, 0, 0, 0.5), builder: (BuildContext context) {
                                            return Container(
                                              child: Column(
                                                mainAxisAlignment: MainAxisAlignment.end,
                                                children: [
                                                  Expanded(child: Container()),
                                                  Container(
                                                    child: Column(
                                                      children: [
                                                        Container(
                                                          child: Text('您还不是会员',style: TextStyle(
                                                              color: Colors.black,
                                                              fontSize: 35.sp,
                                                              fontWeight: FontWeight.w700
                                                          ),),
                                                          margin: EdgeInsets.only(bottom: 20.w),
                                                        ),
                                                        Container(
                                                          child: Text('请开通后查看',style: TextStyle(
                                                              color: Color(0xff242424),
                                                              fontSize: 30.sp,
                                                              fontWeight: FontWeight.w500
                                                          ),),
                                                        ),
                                                        Container(
                                                          margin: EdgeInsets.symmetric(vertical: 40.w,horizontal: 0.w),
                                                          height: 1,
                                                          color: Colors.grey,
                                                        ),
                                                        GestureDetector(
                                                          onTap: (){
                                                            SmartDialog.dismiss();
                                                            Navigator.of(context).pushNamed("MinVipPage").then((value){
                                                              refreshUserInfo();
                                                            });
                                                          },
                                                          child: Container(
                                                            // padding: EdgeInsets.symmetric(horizontal: 60.w),
                                                            child: Row(
                                                              children: [
                                                                Expanded(child: Text('开通会员',textAlign: TextAlign.left,style: TextStyle(
                                                                    color: Color(0xff232323),
                                                                    fontSize: 30.sp,
                                                                    fontWeight: FontWeight.w600
                                                                ),)),
                                                                Icon(Icons.arrow_forward_ios,size: 30.sp,)
                                                              ],
                                                            ),
                                                          ),
                                                        ),
                                                        GestureDetector(
                                                          onTap: (){
                                                            SmartDialog.dismiss();
                                                          },
                                                          child: Container(
                                                            width: 1.sw-120.w,
                                                            height: 90.w,
                                                            alignment: Alignment.center,
                                                            margin: EdgeInsets.only(top: 50.w),
                                                            decoration: BoxDecoration(
                                                                color: Color(0xffEBEBEB),
                                                                borderRadius: BorderRadius.circular(10.w)
                                                            ),
                                                            child: Text('取消',style: TextStyle(
                                                                color: Color(0xff232323),
                                                                fontSize: 30.sp,
                                                                fontWeight: FontWeight.w500
                                                            ),),
                                                          ),
                                                        )
                                                      ],
                                                    ),
                                                    decoration: BoxDecoration(
                                                        color: Colors.white,
                                                        borderRadius: BorderRadius.only(topLeft: Radius.circular(15.w),topRight: Radius.circular(15.w))
                                                    ),
                                                    padding: EdgeInsets.only(left: 60.w,right: 60.w,top: 30.w,bottom: 20.w+ScreenUtil().bottomBarHeight),
                                                  )
                                                ],
                                              ),
                                            );
                                        },
                                        );
                                        return;
                                      }
                                      else if(userInfo.card!=1){
                                        showToast('您还未实名认证，请认证成功后可以联系对方');
                                        return;
                                      }
                                      else if(user.vip!=1){
                                        showToast('当前商家未注册会员，正在通知商家');
                                        await HomeAPI.sendMsgToShopHome(user.phone!);
                                        return;
                                      }
                                      launchUrl(Uri.parse('tel:${user.phone}'));
                                    },
                                    child: Container(
                                      alignment: Alignment.center,
                                      child: Image.asset('assets/min/min_lxwm.png',width: 280.w,),
                                    ),
                                  ),
                                  visible: true,
                                ),
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                    Positioned(child: Container(
                      child: Row(
                        children: [
                          GestureDetector(
                            onTap: (){
                              Navigator.of(context).pop({'attentionState':user.attentionState});
                            },
                            child: Container(
                              width: 100.w,
                              height: 100.w,
                              alignment: Alignment.centerLeft,
                              child: Icon(Icons.arrow_back_ios,color: Colors.white,size: 40.w,),
                            ),
                          ),
                          Expanded(child: Container(
                            alignment: Alignment.center,
                            child: Text('商家主页',style: TextStyle(
                                color: Colors.white,
                                fontSize: 36.sp,
                                fontWeight: FontWeight.w600
                            ),),
                          )),
                          Container(
                              width: 100.w,
                            height: 100.w,
                          ),
                        ],
                      ),
                    ),top: ScreenUtil().statusBarHeight,left: 30.w,right: 30.w,),
                  ],
                ),
              ),
            ),
            SliverAppBar(
              leading: Container(),
              backgroundColor: bgColor,
              pinned:true,
              floating: true,
              primary: false,
              toolbarHeight: tabHeight,
              collapsedHeight: tabHeight,
              bottom: PreferredSize(
                preferredSize: Size(1.sw,80.w),
                child: Container(
                  height: 80.w,
                  color: Colors.white,
                  child: TabBar(
                    controller: _tabController,
                    indicatorColor: Color(0xffEA6B2B),
                    unselectedLabelColor: Color(0xff1A051D),
                    unselectedLabelStyle: TextStyle(
                      fontSize: 30.sp,
                      color: Color(0xff1A051D),
                    ),
                    labelColor: Color(0xffEA6B2B),
                    labelStyle: TextStyle(
                      fontSize: 30.sp,
                      color: Color(0xffEA6B2B),
                      fontWeight: FontWeight.w500,
                    ),
                    tabs: const [
                      Text("作品"),
                      Text("现货"),
                      Text("意向"),
                      Text('收藏')
                    ],
                  ),
                ),
              ),
            ),
          ];
        },
        body: Container(
          child: TabBarView(
            controller: _tabController,
            children:  [
              MinVideoPage(sTyep: 1,isMine:false,uid: widget.uid,),
              MinXianHuoPage(isMine:false,uid: widget.uid,),
              MinVideoPage(sTyep: 2,isMine:false,uid: widget.uid,),
              MinCollectionPage(isMine:false,uid: widget.uid,),
            ],
          ),
        ),
      ),
    );
  }
}
